﻿using System;
using System.IO;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;

namespace PickGold.Nulls
{
	/// <summary>
	/// NullDictionary
	/// </summary>
	public sealed class NullDictionary : IDictionary
	{
		class DictionaryEnumerator : IDictionaryEnumerator
		{
			public readonly static IDictionaryEnumerator Value = new DictionaryEnumerator();

			private DictionaryEnumerator()
			{
				if (Value != null)
					throw new InvalidOperationException();
			}

			DictionaryEntry IDictionaryEnumerator.Entry
			{
				get
				{
					return new DictionaryEntry();
				}
			}

			object IDictionaryEnumerator.Key
			{
				get
				{
					return null;
				}
			}

			object IDictionaryEnumerator.Value
			{
				get
				{
					return null;
				}
			}

			object IEnumerator.Current
			{
				get
				{
					return null;
				}
			}

			bool IEnumerator.MoveNext()
			{
				return false;
			}

			void IEnumerator.Reset()
			{
			}
		}

		/// <summary>
		/// 唯一实例
		/// </summary>
		public readonly static IDictionary Value = new NullDictionary();

		private NullDictionary()
		{
			if (Value != null)
				throw new InvalidOperationException();
		}

		void IDictionary.Add(object key, object value)
		{
		}

		void IDictionary.Clear()
		{
		}

		bool IDictionary.Contains(object key)
		{
			return false;
		}

		IDictionaryEnumerator IDictionary.GetEnumerator()
		{
			return DictionaryEnumerator.Value;
		}

		bool IDictionary.IsFixedSize
		{
			get
			{
				return false;
			}
		}

		bool IDictionary.IsReadOnly
		{
			get
			{
				return false;
			}
		}

		ICollection IDictionary.Keys
		{
			get
			{
				return NullCollection.Value;
			}
		}

		void IDictionary.Remove(object key)
		{
		}

		ICollection IDictionary.Values
		{
			get
			{
				return NullCollection.Value;
			}
		}

		object IDictionary.this[object key]
		{
			get
			{
				return null;
			}
			set
			{
			}
		}

		void ICollection.CopyTo(Array array, int index)
		{
		}

		int ICollection.Count
		{
			get
			{
				return 0;
			}
		}

		bool ICollection.IsSynchronized
		{
			get
			{
				return true;
			}
		}

		object ICollection.SyncRoot
		{
			get
			{
				return this;
			}
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return NullEnumerator.Value;
		}
	}

	/// <summary>
	/// NullDictionary
	/// </summary>
	public sealed class NullDictionary<TKey, TValue> : IDictionary<TKey, TValue>
	{
		class DictionaryEnumerator : IDictionaryEnumerator
		{
			public readonly static IDictionaryEnumerator Value = new DictionaryEnumerator();

			private DictionaryEnumerator()
			{
				if (Value != null)
					throw new InvalidOperationException();
			}

			DictionaryEntry IDictionaryEnumerator.Entry
			{
				get
				{
					return new DictionaryEntry();
				}
			}

			object IDictionaryEnumerator.Key
			{
				get
				{
					return null;
				}
			}

			object IDictionaryEnumerator.Value
			{
				get
				{
					return null;
				}
			}

			object IEnumerator.Current
			{
				get
				{
					return null;
				}
			}

			bool IEnumerator.MoveNext()
			{
				return false;
			}

			void IEnumerator.Reset()
			{
			}
		}

		/// <summary>
		/// 唯一实例
		/// </summary>
		public readonly static IDictionary<TKey, TValue> Value = new NullDictionary<TKey, TValue>();

		private NullDictionary()
		{
			if (Value != null)
				throw new InvalidOperationException();
		}

		void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
		{
		}

		bool IDictionary<TKey, TValue>.ContainsKey(TKey key)
		{
			return false;
		}

		ICollection<TKey> IDictionary<TKey, TValue>.Keys
		{
			get
			{
				return NullCollection<TKey>.Value;
			}
		}

		bool IDictionary<TKey, TValue>.Remove(TKey key)
		{
			return false;
		}

		bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value)
		{
			value = Reflector<TValue>.DefaultValue;
			return false;
		}

		ICollection<TValue> IDictionary<TKey, TValue>.Values
		{
			get
			{
				return NullCollection<TValue>.Value;
			}
		}

		TValue IDictionary<TKey, TValue>.this[TKey key]
		{
			get
			{
				return Reflector<TValue>.DefaultValue;
			}
			set
			{
			}
		}

		void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
		{
		}

		void ICollection<KeyValuePair<TKey, TValue>>.Clear()
		{
		}

		bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
		{
			return false;
		}

		void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
		{
		}

		int ICollection<KeyValuePair<TKey, TValue>>.Count
		{
			get
			{
				return 0;
			}
		}

		bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
		{
			get
			{
				return false;
			}
		}

		bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
		{
			return false;
		}

		IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
		{
			return NullEnumerator<KeyValuePair<TKey, TValue>>.Value;
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return NullEnumerator<KeyValuePair<TKey, TValue>>.Value;
		}
	}
}
